package com.hw.one.dam2.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hw.one.dam2.util.MathCalculateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.hw.one.core.controller.BaseController;
import com.hw.one.core.result.MsgResult;
import com.hw.one.dam2.common.DamConstant;
import com.hw.one.dam2.service.IPatrolDeviceService;
import com.hw.one.dam2.util.CalendarUtil;
import com.hw.one.ext.util.UserUtil;

/**
 * 巡检轨迹 控制层
 * 
 * @author alicke
 *
 */
@RestController
@RequestMapping(value = "patrolTrack")
public class PatrolTrackController extends BaseController{

	@Autowired
	private IPatrolDeviceService patrolDeviceService;
	
	/**
	 * 获取巡检设备信息
	 * 
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getPatrolDeviceInfo", method = RequestMethod.GET)
	public MsgResult getPatrolDeviceInfo(Model model, HttpServletRequest request){
		try {
			
			String deviceType = request.getParameter("deviceType");
			deviceType = null==deviceType || "".equals(deviceType) ? "allDeviceType":deviceType;
			
			Map<String,Object> paraMap = new HashMap<String,Object>();
			paraMap.put("regionCode", "410100000");
			paraMap.put("deviceType", deviceType);
			
			List<Map<String,Object>> patrolDeviceList = patrolDeviceService.findPatrolDeviceInfoByRegionCode(paraMap);
			return MsgResult.okResult(patrolDeviceList);
		} catch (Exception e) {
			e.printStackTrace();
			return MsgResult.exceptionResult();
		}
	}


	/**
	 * 获取巡检设备信息列表
	 * @author 李亚坤
	 * @createtime 2017年5月12日09:41:39
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getPatrolDeviceList", method = RequestMethod.GET)
	public MsgResult getPatrolDeviceList(HttpServletRequest request) {
		try {
			String deviceType = request.getParameter("deviceType");
			String deviceCode = request.getParameter("deviceCode");
			deviceType = StringUtils.isEmpty(deviceType) || !MathCalculateUtil.isInteger(deviceType) ? "0":deviceType;

			Map<String,Object> paraMap = new HashMap<String,Object>();
			// 获取登录用户所属区域信息
			Map<String, Object> region = UserUtil.getRegion();
			// 用户所属区域
			String userRegionCode = (String) region.get("regionCode");
			paraMap.put("regionCode", userRegionCode);
			paraMap.put("deviceType", deviceType);
			paraMap.put("deviceCode", deviceCode);

			List<Map<String,Object>> patrolDeviceList = patrolDeviceService.findPatrolDeviceListByRegionCode(paraMap);
			return MsgResult.okResult(patrolDeviceList);
		}catch (Exception e) {
			e.printStackTrace();
			return MsgResult.exceptionResult();
		}
	}

	/**
	 * 查询巡检设备的轨迹信息
	 * @author 李亚坤
	 * @createtime 2017年5月12日11:25:38
	 * @return
	 */
	@RequestMapping(value = "findPatrolDeviceTrack", method = RequestMethod.GET)
	public MsgResult findPatrolDeviceTrack(HttpServletRequest request) {
		try {


			Map<String, Object> paramMap = new HashMap<>();
			String startTime = request.getParameter("startTime");
			String endTime = request.getParameter("endTime");
			String deviceCode = request.getParameter("deviceCode");
			if (StringUtils.isEmpty(deviceCode)) {
				return MsgResult.exceptionResult();
			}
			if (StringUtils.isEmpty(startTime)) {
				startTime = CalendarUtil.getDateFormat(new Date(), DamConstant.ALL_SDF_YMD);
			}
			if (StringUtils.isEmpty(endTime)) {
				endTime = CalendarUtil.getDateFormat(new Date(), DamConstant.SDF_YMD) + " 23:59:59";
			}
			paramMap.put("startTime", startTime);
			paramMap.put("endTime", endTime);
			paramMap.put("deviceCode", deviceCode);
			Map<String, Object> returnMap = patrolDeviceService.findPatrolDeviceTrack(paramMap);
			return MsgResult.okResult(returnMap);
		}catch (Exception e) {
			e.printStackTrace();
			return MsgResult.exceptionResult();
		}
	}


	/**
	 * 返回当前用户能看到的巡检社保的类型的数量，现在返回的是渣土车
	 * @author songjundan
	 * @createtime 2017年8月2日17:25:38
	 * @return
	 */
	@RequestMapping(value = "deviceType/num", method = RequestMethod.GET)
	public MsgResult findDeviceNumByRegionCodeAndType(HttpServletRequest request){
		Map<String, Object> returnMap = new HashMap<>();
		Map<String, Object> paraMap = wrapParams(request);
		Map<String, Object> region = UserUtil.getRegion();
		String regionCode = (String) region.get("regionCode");  //获取用户的regionCode
		paraMap.put("regionCode", regionCode);
		paraMap.put("deviceType", "4");//返回的是杂土车
		try {
			List<Map<String,Object>> list = patrolDeviceService.findDeviceNumByRegionCodeAndType(paraMap);
			if(list != null && list.size()>0){
				returnMap.put("4", list.size());
			}else{
				returnMap.put("4", 0);
			}
			
			return MsgResult.okResult(returnMap);
		}catch (Exception e) {
			e.printStackTrace();
			return MsgResult.exceptionResult();
		}
	}


	/**
	 * 查询巡检设备的类型列表，
	 * 	deviceType为前台显示的名称；
	 *	deviceCode 为数据库对应类型
	 *		0为全部
	 *		-1为无效类型
	 * @return
	 */
	@RequestMapping(value = "findPatrolTrackTypeList", method = RequestMethod.GET)
	public MsgResult findPatrolTrackTypeList() {
		try {
			JSONArray resultList = new JSONArray();
			JSONObject obj = new JSONObject();
			obj.put("deviceType", "全部");
			obj.put("deviceCode", "0");
			resultList.add(obj);
			obj = new JSONObject();
			obj.put("deviceType", "渣土车");
			obj.put("deviceCode", "4");
			resultList.add(obj);
			obj = new JSONObject();
			obj.put("deviceType", "执法车");
			obj.put("deviceCode", "-1");	// 无数据，暂时不定类型
			resultList.add(obj);
			obj = new JSONObject();
			obj.put("deviceType", "网格员");
			obj.put("deviceCode", "-1");	// 无数据，暂时不定类型
			resultList.add(obj);
			obj = new JSONObject();
			obj.put("deviceType", "洒水车");
			obj.put("deviceCode", "-1");	 // 无数据，暂时不定类型
			resultList.add(obj);
			obj = new JSONObject();
			obj.put("deviceType", "清扫车");
			obj.put("deviceCode", "-1");	// 无数据，暂时不定类型
			resultList.add(obj);
			obj = new JSONObject();
			obj.put("deviceType", "水泥罐车");
			obj.put("deviceCode", "-1");	// 无数据，暂时不定类型
			resultList.add(obj);
			return MsgResult.okResult(resultList);
		}catch (Exception e) {
			e.printStackTrace();
			return MsgResult.exceptionResult();
		}
	}


}
